package biz;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import dao.UserDao;
import dto.UserDto;
import entity.User;
import util.Config;
import util.ConnectFactory;

public class UserBiz {
	UserDao userDao = new UserDao();
	public List<User> queryUser(int curpage){
		List<User> list = new ArrayList<User>();
		try {
			Connection conn = ConnectFactory.getConnection();
			int begin = (curpage-1)*Config.PageSize;
			list = userDao.queryUser(begin,conn);
			conn.close();
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public int getPageCounts() {
		int result = 0;
		try {
			Connection conn = ConnectFactory.getConnection();
			int a = userDao.queryCount(conn);
			if(a%Config.PageSize==0)
				result = a/Config.PageSize;
			else
			{
				result = a/Config.PageSize+1;
			}
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;		
	}
	
	
	public boolean createUser(String name,int age,int sex,int did) {
		boolean flag = false;
		try {
			Connection conn = ConnectFactory.getConnection();
			int result = userDao.createUser(name, age, sex,did, conn);
			if(result>0)
				flag = true;
			conn.close();
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public boolean deleteUserById(int id) {
		boolean flag = false;
		try {
			Connection conn = ConnectFactory.getConnection();
			int result = userDao.deleteUserById(id, conn);
			if(result>0)
				flag = true;
			conn.close();
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public User getUserById(int id) {
		User user = new User();
		try {
			Connection conn = ConnectFactory.getConnection();
			user = userDao.selectUserById(id, conn);
			conn.close();
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		return user;
	}
	
	public boolean updateUserById(int id,String name,int age,int sex,int did) {
		boolean flag = false;
		try {
			Connection conn = ConnectFactory.getConnection();
			int result = userDao.updateUserById(id, name, age, sex, did, conn);
			if(result>0)
				flag = true;
			conn.close();
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	public List<UserDto> searchUser(String name,String sex,String beginage,String endage,String did,int curpage){
		List<UserDto> list = new ArrayList<UserDto>();
		try {
			Connection conn = ConnectFactory.getConnection();
			int begin = (curpage - 1) * Config.PageSize; 
			list = userDao.searchUser(name, sex, beginage, endage, did, begin, conn);
			conn.close();
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public int searchPageCount(String name,String sex,String beginage,String endage,String did){
		int result = 0;
		try {
			Connection conn = ConnectFactory.getConnection();
			int count = userDao.searchCount(name, sex, beginage, endage, did, conn);
			if(count % Config.PageSize == 0)
				result = count / Config.PageSize;
			else
				result = count / Config.PageSize + 1;
			conn.close();
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}